﻿#include "StrayCat.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    StrayCat::StrayCat(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool StrayCat::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }


        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void StrayCat::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        bool range = !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

        if (!range && getEnvir()->Random->Next(10) > 0)
        {
            if (getEnvir()->Random->Next(10) > 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                Broadcast(tempVar);

                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);
                Attack2();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->Type = 2;
            Broadcast(tempVar3);
            LineAttack(2, 1000);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }

    }

    void StrayCat::Attack2()
    {
        int levelGap = 5;
        int mobLevel = this->getLevel();
        int targetLevel = getTarget()->getLevel();

        if ((targetLevel <= mobLevel + levelGap))
        {
            if (getTarget()->Pushed(this, Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()), 1) > 0)
            {
                //Broadcast(new S.ObjectAttack { ObjectID = ObjectID, Direction = Direction, Location = CurrentLocation, Type = 2 });
                LineAttack(2, 1000);
            }
        }
    }

    void StrayCat::LineAttack(int distance, int additionalDelay, DefenceType defenceType, bool push)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * 2);
        if (damage == 0)
        {
            return;
        }

        for (int i = 1; i <= distance; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), i);

            if (!getCurrentMap()->ValidPoint(target))
            {
                continue;
            }

            Cell *cell = getCurrentMap()->GetCell(target);
            if (cell->Objects.empty())
            {
                continue;
            }

            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                {
                    if (!ob->IsAttackTarget(this))
                    {
                        continue;
                    }

                    if (push)
                    {
                        ob->Pushed(this, getDirection(), distance - 1);
                    }

                    int delay = Functions::MaxDistance(getCurrentLocation(), ob->getCurrentLocation()) * 50 + additionalDelay; //50 MS per Step
                    DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + delay, {ob, damage, defenceType});
                    ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                }
                else
                {
                    continue;
                }

                break;
            }
        }
    }
}
